<?php

namespace app\admin\controller;

use controller\BasicAdmin;
use service\DataService;
use service\ToolsService;
use think\Db;
use think\exception\HttpResponseException;
use think\facade\Cache;

/**
 * 投票记录
 */
class VoteRecord extends BasicAdmin
{

    public $info = 'tp_vote_record';

    /**
     * 投票列表
     * @return array|string
     */
    public function lists()
    {
        $this->title = '投票管理';
        $get = $this->request->get();
        $db = Db::name($this->info)
            ->alias('a')
            ->field('a.*');
//            ->where(['a.is_deleted' => '0']);

        foreach (['item_id', 'wecha_id'] as $field) {
            if (isset($get[$field]) && $get[$field] !== '') {
                $db->where('a.' . $field, "{$get[$field]}");
            }
        }

//        $db->select();

//        print_dump($actions = Db::name($this->info)
//            ->group('area')
//            ->column('area'));
//        print_dump($db->select());

        if (isset($get['touch_time']) && $get['touch_time'] !== '') {
            list($start, $end) = explode(' - ', $get['touch_time']);
            $start = strtotime($start . '00:00:00');
            $end = strtotime($end . ' 23:59:59');
            $db->whereBetween('a.' . 'touch_time', ["{$start}", "{$end}"]);
        }
        return parent::_list($db->order('a.id desc'), 'id');
    }

    public function ticket_detail()
    {
        $vote_item_id = $this->request->get('id');
        $vote = Db::name('tp_vote_item')
            ->alias('a')
            ->join('tp_region b', 'b.id=a.province_id', 'left')
            ->field('a.*,b.name province_name')
            ->find($vote_item_id);

        $province_name = str_replace(['省', '市', '壮族', '回族', '维吾尔', '特别行政区', '自治区'], '', $vote["province_name"]);
        echo '<td style="text-align: left;">' . $vote['item'] . '</td>';
        echo '<table style="margin-left: 50px;margin-top: 20px;;margin-bottom: 50px;"><th style="width:180px;">时间区间</th><th style="width:30px;">票数</th><th style="width:50px;">省内票</th></tr>';

        $time = time();
        $end = false;
        while (!$end) {
            $table_name = 'tp_vote_record';
            $end = true;

            $sql1 = "select DATE_FORMAT(FROM_UNIXTIME(touch_time),'20%y-%m-%d %H:00:00') as _a ,count(1) as _c1
                from $table_name
                WHERE item_id=$vote_item_id
                GROUP BY _a
                ORDER BY _a DESC";

            $sql2 = "select DATE_FORMAT(FROM_UNIXTIME(touch_time),'20%y-%m-%d %H:00:00') as _a ,count(1) as _c2
                from $table_name
                WHERE item_id=$vote_item_id AND (area LIKE '%{$province_name}%')
                GROUP BY _a
                ORDER BY _a DESC";

            $list_all = Db::query($sql1);
            $list_inArea = Db::query($sql2);

            foreach ($list_all as &$item) {
                $item['_c2'] = 0;
                foreach ($list_inArea as $item_area) {
                    if ($item['_a'] == $item_area['_a']) {
                        $item['_c2'] = $item_area['_c2'];
                        break;
                    }
                }
            }

            foreach ($list_all as $_item) {
                echo '<tr>';
                echo '<td style="text-align: left;">' . $_item['_a'] . '</td>';
                echo '<td style="text-align: center;">' . $_item['_c1'] . '</td>';
                echo '<td style="text-align: center;">' . $_item['_c2'] . '</td>';
                echo '</tr>';
            }

            $time -= 3600 * 24;
        }

        echo '</table>';
    }


    public function ticket_detail_top10()
    {

        $list_top10 = Db::name('tp_vote_item')
            ->alias('a')
            ->join('tp_region b', 'b.id=a.province_id', 'left')
            ->field('a.*,b.name province_name')
            ->order('a.vcount desc')
            ->limit(10)
            ->select();

        foreach ($list_top10 as &$one) {
            $vote_item_id = $one['id'];
            $province_name = str_replace(['省', '市', '壮族', '回族', '维吾尔', '特别行政区', '自治区'], '', $one["province_name"]);
            echo '<td style="text-align: left;">' . $one['item'] . '</td>';
            echo '<table style="margin-left: 50px;margin-top: 20px;;margin-bottom: 50px;"><th style="width:180px;">时间区间</th><th style="width:30px;">票数</th><th style="width:50px;">' . $province_name . '地区票</th></tr>';

            $time = time();
            $end = false;
            while (!$end) {
                $table_name = 'tp_vote_record';
                $end = true;

                $sql1 = "select DATE_FORMAT(FROM_UNIXTIME(touch_time),'20%y-%m-%d %H:00:00') as _a ,count(1) as _c1
                from $table_name
                WHERE item_id=$vote_item_id
                GROUP BY _a
                ORDER BY _a DESC";

                $sql2 = "select DATE_FORMAT(FROM_UNIXTIME(touch_time),'20%y-%m-%d %H:00:00') as _a ,count(1) as _c2
                from $table_name
                WHERE item_id=$vote_item_id AND (area LIKE '%{$province_name}%')
                GROUP BY _a
                ORDER BY _a DESC";

                $list_all = Db::query($sql1);
                $list_inArea = Db::query($sql2);

                foreach ($list_all as &$item) {
                    $item['_c2'] = 0;
                    foreach ($list_inArea as $item_area) {
                        if ($item['_a'] == $item_area['_a']) {
                            $item['_c2'] = $item_area['_c2'];
                            break;
                        }
                    }
                }

                foreach ($list_all as $_item) {
                    echo '<tr>';
                    echo '<td style="text-align: left;">' . $_item['_a'] . '</td>';
                    echo '<td style="text-align: center;">' . $_item['_c1'] . '</td>';
                    echo '<td style="text-align: center;">' . $_item['_c2'] . '</td>';
                    echo '</tr>';
                }

                $time -= 3600 * 24;
            }

            echo '</table>';

        }
    }


    /**
     * 列表数据处理
     * @param array $data
     * @throws \Exception
     */
    protected function _data_filter(&$data)
    {
        $ip = new \Ip2Region();
        foreach ($data as &$vo) {
            $result = $ip->btreeSearch($vo['ip']);
            $vo['isp'] = isset($result['region']) ? $result['region'] : '';
            $vo['isp'] = str_replace(['内网IP', '0', '|'], '', $vo['isp']);
        }
    }

}
